package com.woniuxy.mqconsumer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OrderDelayMqConfiguration {
    public static final String ORDER_NORMAL_EXCHANGE = "order_normal_exchange" ;
    public static final String ORDER_DEAD_EXCHANGE = "order_dead_exchange";
    public static final String ORDER_NORMAL_QUEUE = "order_normal_queue";
    public static final String ORDER_DEAD_QUEUE = "order_dead_queue";
    public static final String ORDER_NORMAL_ROUTING_KEY = "order_normal_routing_key" ;
    public static final String ORDER_DEAD_ROUTING_KEY = "order_dead_routing_key" ;

    //创建正常交换机
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(ORDER_NORMAL_EXCHANGE,true,false);
    }
    //创建死信交换机
    @Bean
    public DirectExchange deadExchange(){
        return new DirectExchange(ORDER_DEAD_EXCHANGE,true,false);
    }

    //创建正常队列 （重要）
    @Bean
    public Queue queueNormal(){
        return QueueBuilder.durable(ORDER_NORMAL_QUEUE)
                .deadLetterExchange(ORDER_DEAD_EXCHANGE)
                .deadLetterRoutingKey(ORDER_DEAD_ROUTING_KEY)
                .ttl(5*60*1000)
                .build();
    }

    //创建死信队列
    @Bean
    public Queue deadQueue(){
        return QueueBuilder.durable(ORDER_DEAD_QUEUE).build();
    }

    //正常交换机绑定正常队列
    @Bean
    public Binding bindDirectExchangeAndQueueA(){
        return BindingBuilder.bind(queueNormal()).to(directExchange()).with(ORDER_NORMAL_ROUTING_KEY);
    }
    //死信交换机绑定死信队列
    @Bean
    public Binding bindDeadExchangeAndDeadQueue(){
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(ORDER_DEAD_ROUTING_KEY);
    }
}
